//
//  LearningView.m
//  HotWord
//
//  Created by Rainbow on 1/15/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "LearningWordView.h"
#import "TranslationView.h"
#import "Meaning.h"

@interface LearningWordView()

-(void)updateTranslateScrollView;
-(void)updateSynonymScrollView;
-(void)updateAntonymScrollView;
-(void)updateSentenceScrollView;

-(void)synonymChanged;
-(void)antonymChanged;
-(void)sentenceChanged;

- (void)showFadeInAndOutLight:(UIView *)lightView animationId:(NSString *)animationId;

@end

@implementation LearningWordView
@synthesize wordLabel;
@synthesize slugLabel;
@synthesize translateScrollView;
@synthesize synonymScrollView;
@synthesize antonymScrollView;
@synthesize synonymLeftArrow;
@synthesize synonymRightArrow;
@synthesize antonymRightArrow;
@synthesize antonymLeftArrow;
@synthesize sentenceLeftArrow;
@synthesize sentenceRightArrow;
@synthesize pageControl;
@synthesize synonymLight;
@synthesize antonymLight;
@synthesize sentenceLight;
@synthesize sentenceScrollView;
@synthesize word;
@synthesize index;


- (void)updateWord:(Word *)_word
{
    self.word = _word;
    wordLabel.text = self.word.content;
    slugLabel.text = self.word.slug;
    
    [self updateTranslateScrollView];
    [self updateSynonymScrollView];
    [self updateAntonymScrollView];
    [self updateSentenceScrollView];
    
    translateScrollView.delegate = self;
    synonymScrollView.delegate = self;
    antonymScrollView.delegate = self;
    sentenceScrollView.delegate = self;
}

- (void)reset
{
    [translateScrollView removeAllSubviews];
    [synonymScrollView removeAllSubviews];
    [antonymScrollView removeAllSubviews];
    [sentenceScrollView removeAllSubviews];
    
    synonymRightArrow.alpha = 0;
    synonymLeftArrow.alpha = 0;
    antonymRightArrow.alpha = 0;
    antonymLeftArrow.alpha = 0;
    sentenceRightArrow.alpha = 0;
    sentenceLeftArrow.alpha = 0;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if(scrollView == synonymScrollView){
        [self synonymChanged];
    }
    else if(scrollView == antonymScrollView){
        [self antonymChanged];
    }
    else if(scrollView == sentenceScrollView){
        [self sentenceChanged];
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView == translateScrollView) {
        _meaningIndex = self.translateScrollView.contentOffset.x/self.translateScrollView.width;
        pageControl.currentPage = _meaningIndex;
        [synonymScrollView removeAllSubviews];
        [antonymScrollView removeAllSubviews];
        [sentenceScrollView removeAllSubviews];
        [self updateSynonymScrollView];
        [self updateAntonymScrollView];
        [self updateSentenceScrollView];
        [self showFadeInAndOutLight:synonymLight animationId:@"synonym"];
        [self showFadeInAndOutLight:antonymLight animationId:@"antonym"];
        [self showFadeInAndOutLight:sentenceLight animationId:@"sentence"];
    }
}

-(void)updateTranslateScrollView
{
    [self.translateScrollView setContentOffset:CGPointZero];
    _meaningIndex = 0;
    pageControl.numberOfPages = [word.meanings count];
    pageControl.currentPage = _meaningIndex;
    if ([word.meanings count] < 2) {
        pageControl.hidden = YES;
    }
    else
    {
        pageControl.hidden = NO;
    }
    self.translateScrollView.contentSize = CGSizeMake(self.translateScrollView.frame.size.width * [word.meanings count], self.translateScrollView.frame.size.height);
    for (int i = 0; i < [word.meanings count]; i++) {
        Meaning *meaning = [word.meanings objectAtIndex:i];
        TranslationView *translationView = [[[NSBundle mainBundle] loadNibNamed:@"TranslationView" owner:self options:nil] objectAtIndex:0];
        [translationView setChinese:meaning.chineseTranslate andEnglish:meaning.englishTranslate];
        translationView.frame = CGRectMake(i*self.translateScrollView.width, 0, translationView.frame.size.width, translationView.frame.size.height);
        [self.translateScrollView addSubview:translationView];
    }
}
-(void)updateSynonymScrollView
{
    
    [self.synonymScrollView setContentOffset:CGPointZero];
    if ([word.meanings count] == 0) {
        [self.synonymScrollView setContentSize:CGSizeZero];
        return;
    }
    Meaning *meaning = [word.meanings objectAtIndex:_meaningIndex];
    self.synonymScrollView.contentSize = CGSizeMake(self.synonymScrollView.frame.size.width * [meaning.synonyms count], self.synonymScrollView.frame.size.height);
    for (int i = 0; i < [meaning.synonyms count]; i++) {
        UILabel *contentLabel = [[[UILabel alloc] initWithFrame:CGRectMake(i*self.synonymScrollView.width, 5, self.synonymScrollView.width, 20)] autorelease];
        contentLabel.backgroundColor = [UIColor clearColor];
        contentLabel.text = [meaning.synonyms objectAtIndex:i];
        contentLabel.textAlignment = UITextAlignmentCenter;
        contentLabel.font = [UIFont fontWithName:@"Verdana-Italic" size:14];
        [self.synonymScrollView addSubview:contentLabel];
    }
    
    synonymLeftArrow.alpha = 0;
    if ([meaning.synonyms count] > 1) {
        synonymRightArrow.alpha = 1;
    }
    else
    {
        synonymRightArrow.alpha = 0;
    }
}

-(void)updateAntonymScrollView
{
    
    [self.antonymScrollView setContentOffset:CGPointZero];
    if ([word.meanings count] == 0) {
        [self.antonymScrollView setContentSize:CGSizeZero];
        return;
    }
    Meaning *meaning = [word.meanings objectAtIndex:_meaningIndex];
    self.antonymScrollView.contentSize = CGSizeMake(self.antonymScrollView.frame.size.width * [meaning.antonyms count], self.antonymScrollView.frame.size.height);
    for (int i = 0; i < [meaning.antonyms count]; i++) {
        UILabel *contentLabel = [[[UILabel alloc] initWithFrame:CGRectMake(i*self.antonymScrollView.width, 5, self.antonymScrollView.width, 20)] autorelease];
        contentLabel.backgroundColor = [UIColor clearColor];
        contentLabel.text = [meaning.antonyms objectAtIndex:i];
        contentLabel.textAlignment = UITextAlignmentCenter;
        contentLabel.font = [UIFont fontWithName:@"Verdana-Italic" size:14];
        [self.antonymScrollView addSubview:contentLabel];
    }
    
    antonymLeftArrow.alpha = 0;
    if ([meaning.antonyms count] > 1) {
        antonymRightArrow.alpha = 1;
    }
    else
    {
        antonymRightArrow.alpha = 0;
    }
}
-(void)updateSentenceScrollView
{
    
    [self.sentenceScrollView setContentOffset:CGPointZero];
    if ([word.meanings count] == 0) {
        [self.sentenceScrollView setContentSize:CGSizeZero];
        return;
    }
    Meaning *meaning = [word.meanings objectAtIndex:_meaningIndex];
    self.sentenceScrollView.contentSize = CGSizeMake(self.sentenceScrollView.frame.size.width * [meaning.sentences count], self.sentenceScrollView.frame.size.height);
    for (int i = 0; i < [meaning.sentences count]; i++) {
        UITextView *contentView = [[[UITextView alloc] initWithFrame:CGRectMake(i*self.sentenceScrollView.width, 0 , self.sentenceScrollView.width, 200)] autorelease];
        contentView.text = [meaning.sentences objectAtIndex:i];
        contentView.backgroundColor = [UIColor clearColor];
        contentView.textAlignment = UITextAlignmentLeft;
        contentView.font = [UIFont fontWithName:@"Georgia-Italic" size:12];
        contentView.editable = NO;
        NSInteger contentHeight = contentView.contentSize.height;
        
        if (contentHeight >= self.sentenceScrollView.height) {
            contentHeight = self.sentenceScrollView.height;
        }
        
        contentView.height = contentHeight;
        contentView.top = (self.sentenceScrollView.height - contentHeight)/2;
        
        [self.sentenceScrollView addSubview:contentView];
    }
    
    sentenceLeftArrow.alpha = 0;
    if ([meaning.sentences count] > 1) {
        sentenceRightArrow.alpha = 1;
    }
    else
    {
        sentenceRightArrow.alpha = 0;
    }
}

-(void)synonymChanged
{
    Meaning *meaning = [word.meanings objectAtIndex:_meaningIndex];
    NSInteger showingIndex = self.synonymScrollView.contentOffset.x/self.synonymScrollView.width;
    if (showingIndex > 0) {
        [UtilHelper showFadeInAnimation:self.synonymLeftArrow endAlpha:1];
    }
    else
    {
        [UtilHelper showFadeInAnimation:self.synonymLeftArrow endAlpha:0];
    }
    if (showingIndex < [meaning.synonyms count] - 1) {
        [UtilHelper showFadeInAnimation:self.synonymRightArrow endAlpha:1];
    }
    else
    {
        [UtilHelper showFadeInAnimation:self.synonymRightArrow endAlpha:0];
    }
}

-(void)antonymChanged
{
    Meaning *meaning = [word.meanings objectAtIndex:_meaningIndex];
    NSInteger showingIndex = self.antonymScrollView.contentOffset.x/self.antonymScrollView.width;
    if (showingIndex > 0) {
        [UtilHelper showFadeInAnimation:self.antonymLeftArrow endAlpha:1];
    }
    else
    {
        [UtilHelper showFadeInAnimation:self.antonymLeftArrow endAlpha:0];
    }
    if (showingIndex < [meaning.antonyms count] - 1) {
        [UtilHelper showFadeInAnimation:self.antonymRightArrow endAlpha:1];
    }
    else
    {        
        [UtilHelper showFadeInAnimation:self.antonymRightArrow endAlpha:0];
    }
}
-(void)sentenceChanged
{
    Meaning *meaning = [word.meanings objectAtIndex:_meaningIndex];
    NSInteger showingIndex = self.sentenceScrollView.contentOffset.x/self.sentenceScrollView.width;
    if (showingIndex > 0) {
        [UtilHelper showFadeInAnimation:self.sentenceLeftArrow endAlpha:1];
    }
    else
    {
        [UtilHelper showFadeInAnimation:self.sentenceLeftArrow endAlpha:0];
    }
    if (showingIndex < [meaning.sentences count] - 1) {
        [UtilHelper showFadeInAnimation:self.sentenceRightArrow endAlpha:1];
    }
    else
    {        
        [UtilHelper showFadeInAnimation:self.sentenceRightArrow endAlpha:0];
    }
}

- (void)showFadeInAndOutLight:(UIView *)lightView animationId:(NSString *)animationId
{
    [UIView beginAnimations:animationId context:lightView];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDuration:0.3];
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
    lightView.alpha = 1;
    [UIView commitAnimations];
}

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationDuration:0.3];
    if ([animationID isEqualToString:@"synonym"]) {
        synonymLight.alpha = 0;
    }
    else if([animationID isEqualToString:@"antonym"])
    {
        antonymLight.alpha = 0;
    }
    else if([animationID isEqualToString:@"sentence"])
    {
        sentenceLight.alpha = 0;
    }
    [UIView commitAnimations];
}
     
- (void)dealloc {
    [wordLabel release];
    [slugLabel release];
    [translateScrollView release];
    [synonymScrollView release];
    [antonymScrollView release];
    [synonymLeftArrow release];
    [synonymRightArrow release];
    [antonymRightArrow release];
    [sentenceScrollView release];
    [word release];
    [pageControl release];
    [antonymLeftArrow release];
    [sentenceLeftArrow release];
    [sentenceRightArrow release];
    [synonymLight release];
    [antonymLight release];
    [sentenceLight release];
    [super dealloc];
}
@end
